angular.module('whm.controllers', ['whm.services'])
.controller('AppCtrl', function($scope,$rootScope,$location,$meteor,$state) {
	$rootScope.hasFilter = false;
	$rootScope.showEdit=false;
	$rootScope.isEdit = false;
	$rootScope.toggleEdit=function(){ //callback
		$rootScope.isEdit=!$rootScope.isEdit;
	};
	$rootScope.isFirstLogin = !!$location.search().firstLogin;

})
.controller('HomeCtrl', function($scope, $state) {  //, InventoryService
})

.controller('filterController', function($scope,$rootScope) {
	$rootScope.showFilter = function() {
		$rootScope.filterShow = !$rootScope.filterShow;
	};
	$rootScope.filterDate = function(date) {
		$rootScope.filterShow = !$rootScope.filterShow;
	};
})


// 调货单
.controller('TransferProductsCtrl', function($scope) {
	var productsSubHandle = $scope.subscribe('products');
	$scope.helpers({
		products: function() {
			return Products.find({});
		}
	});

	$scope.autorun(function(){
		if(productsSubHandle.ready() && $scope.products){
			$scope.title = '全部商品';
			$scope.brandList = {};
			$scope.products.forEach(function(product) {
				product.finalPrice = product.sale_price;
				if(!$scope.brandList[product.properties.brand.value]){
					$scope.brandList[product.properties.brand.value]=1;
				}else{
					$scope.brandList[product.properties.brand.value]+=1;
				}
				return;
			});
		}
	});

	$scope.toggleCat=function(){
		$scope.showCat = ! $scope.showCat;
	};

	$scope.toggleItem = function(item) {
    if ($scope.isItemShown(item)) {
      $scope.shownItem = null;
    } else {
      $scope.shownItem = item;
    }
  };
  $scope.isItemShown = function(item) {
    return $scope.shownItem === item;
  };
  $scope.count=1;
  $scope.minusCount = function(){
	$scope.count=$scope.count<2?1:$scope.count-1;
  };
  $scope.addCount = function(){
  	$scope.count=$scope.count+1;
  };

  $scope.selectedProducts=[];
  $scope.selectProduct = function(product){
  	var index = $scope.selectedProducts.indexOf(product);
	if(index === -1){
		$scope.selectedProducts.push(product);
	}else{
		$scope.selectedProducts.splice(index,1);
	}

  };
})


.controller('QueryResultCtrl', function($scope) {

})



.controller('TransferListCtrl', function($scope) {

})



.controller('TransferDetailCtrl', function($scope) {

});
